﻿using System;
using System.Collections.Generic;

using aier.dataaccess.model;
using aier.dataaccess.dapper;
using aier.xrm.wcf;

using Microsoft.Xrm.Sdk;
using aier.wx.nosql.redis;
using log4net;

namespace aier.business.logic
{
    public class CommunityBll
    {
        public CommunityDal communityDal { get; set; }

        public StreetDal streetDal { get; set; }
        public CommonWcf commonWcf { get; set; }
        public RedisTestDal redisTestDal { get; set; }
        public ILog log { get; set; }

        public ResResult resResult { get; set; }
        /// <summary>
        /// 通过视图获取所有社区信息
        /// </summary>
        /// <returns></returns>
        public List<CommunityView> QueryAllCommunity()
        {
            return communityDal.QueryAllCommunity();
        }

        /// <summary>
        /// 根据社区ID查询视图信息
        /// </summary>
        /// <param name="communityId"></param>
        /// <returns></returns>
        public CommunityView QueryCommunityById(string communityId)
        {
            return communityDal.QueryCommunityById(communityId);
            //return communityDal.QueryCommunityById(communityId);
        }

        /// <summary>
        /// 根据社区ID查询视图信息
        /// </summary>
        /// <param name="communityId"></param>
        /// <returns></returns>
        public List<CommunityView> QueryCommunityByName(string communityName)
        {
            return communityDal.QueryCommunityByName(communityName);
        }
        /// <summary>
        /// 根据不同条件查询社区对象,只要属性不为空就视为查询条件
        /// 查询条件为：社区ID/省份ID/市ID/区ID/CountyID/街道ID/名称(模糊搜索)
        /// 原程序 LocationQueryServiceImpl/queryCommunityByStreet
        /// </summary>
        /// <param name="communityView">社区对象</param>
        /// <returns></returns>
        public List<CommunityView> QueryCommunityByStreet(CommunityView communityView)
        {
            List<CommunityView> communityViews = null;
            if (communityView.new_cityName == "全部") communityView.new_cityName = string.Empty;
            if (communityView.new_countyName == "全部") communityView.new_countyName = string.Empty;
            communityViews = communityDal.QueryCommunityByStreet(communityView);
            return communityViews;
        }
        /// <summary>
        /// 根据不同条件查询社区数量,只要属性不为空就视为查询条件
        /// 查询条件为：社区ID/省份ID/市ID/区ID/CountyID/街道ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCommunityCount
        /// </summary>
        /// <param name="communityView">社区对象</param>
        /// <returns></returns>
        public int QueryCommunityCount(CommunityView communityView)
        {
            int count = communityDal.QueryCommunityCount(communityView);
            return count;
        }
        /// <summary>
        /// 根据社区ID修改社区信息
        /// </summary>
        /// <param name="communityJson"></param>
        /// <returns></returns>
        public ResResult UpdateCommunityById(Community community)
        {
            ResResult resResult = new ResResult();
            try
            {
                Guid contactid = ContactCreateOrUpadate(community);
                community.new_contact = contactid;
                Entity entity = community.AsCrmEntity();
                if (community.userid == Guid.Empty)
                {
                    commonWcf.UpdateEntityById(entity);
                }
                else
                {
                    commonWcf.UpdateEntityProxyById(community.userid, entity);
                }
              return  resResult.SuccessResult();
            }
            catch (Exception ex)
            {
                return resResult.FailResult(ex.Message);
            }
            

            //new CommunityWcf().UpdateCommunityById(entity);
        }

        /// <summary>
        /// 创建社区
        /// </summary>
        /// <param name="entity"></param>
        public ResResult CreateCommuntity(Community community)
        {
            ResResult resResult = new ResResult();
            try
            {
                Guid targetid = Guid.Empty;
                if (community.new_street != Guid.Empty)
                {
                    StreetView street = streetDal.QueryStreetId(community.new_street.ToString());
                    if (street != null)
                    {
                        community.new_province = street.new_province;
                        community.new_city = street.new_city;
                        community.new_county = street.new_area;
                    }

                }
                Guid contactid = ContactCreateOrUpadate(community);
                community.new_contact = contactid;
                Entity entity = community.AsCrmEntity();
                if (community.userid == Guid.Empty)
                {

                    targetid = commonWcf.CreateEntity(entity);
                }
                else
                {
                    targetid = commonWcf.CreateEntityProxy(community.userid, entity);
                }
             return   resResult.SuccessResult(targetid);
            }
            catch (Exception ex)
            {
                return resResult.FailResult(ex.Message);
            }
        }
        /// <summary>
        /// 创建或更新联系人姓名
        /// </summary>
        /// <param name="community"></param>
        /// <returns></returns>
        private Guid ContactCreateOrUpadate(Community community)
        {
            var contactid = Guid.Empty;
            try
            {
                Entity contact = new Entity("contact");
                if (!string.IsNullOrWhiteSpace(community.contactname))
                {
                    contact["lastname"] = community.contactname;
                }
                if (community.new_contact == Guid.Empty|| community.new_contact==null)
                {
                    if (community.userid == Guid.Empty)
                    {
                        contactid = commonWcf.CreateEntity(contact);
                    }
                    else
                    {
                        contactid = commonWcf.CreateEntityProxy(community.userid, contact);
                    }
                }
                else
                {
                    contact.Id = Guid.Parse(community.new_contact.ToString());
                    if (community.userid == Guid.Empty)
                    {
                        commonWcf.UpdateEntityById(contact);
                        contactid = contact.Id;
                    }
                    else
                    {
                        commonWcf.UpdateEntityProxyById(community.userid, contact);
                        contactid = contact.Id;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("CommunityBll对象中，ContactCreateOrUpadate方法："+ex.StackTrace+ex.Message);
            }                      
            return contactid;
        }

        /// <summary>
        /// 社区查询信息
        /// 原程序 ：ScreeningServiceImpl->  listCommunity(String name, String province, String county, String contact, String city, String street, String mobile, String ownerid, Integer statecode)
        /// </summary>
        /// <param name="name">社区名称</param>
        /// <param name="province">省ID</param>
        /// <param name="county">区/县ID</param>
        /// <param name="contact">联系人ID</param>
        /// <param name="city">市ID</param>
        /// <param name="street">街道ID</param>
        /// <param name="mobile">手机</param>
        /// <param name="ownerid">负责人</param>
        /// <param name="statecode">状态</param>
        /// <returns></returns>
        public IEnumerable<CommunityView> QueryCommunityByCondition(string name, string province, string county, string contact, string city, string street, string mobile, string ownerid, int statecode)
        {
            return communityDal.QueryCommunityByCondition( name,  province,  county,  contact,  city,  street,  mobile,  ownerid,  statecode);
        }

        /// <summary>
        /// 社区查询信息
        /// 原程序 ：ScreeningServiceImpl->  listCommunity(String province, String county, String city, String ownerid)
        /// </summary>
        /// <param name="province">省ID</param>
        /// <param name="county">区/县ID</param>
        /// <param name="city">市ID</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        public List<CommunityView> QueryCommunityByPosition(string province, string city, string county, string ownerid,string hospital)
        {
            string keyName = string.Concat(ownerid, hospital);//string.Concat(province, city, county);
            List<CommunityView> communityViews = null;
            //communityViews = redisTestDal.Get<List<CommunityView>>(keyName);
            //communityViews = null;
            if (communityViews != null && communityViews.Count > 0)
            {
                return communityViews;
            }
            else
            {
                city = city == "全部" ? string.Empty : city;
                county = county == "全部" ? string.Empty : county;
                communityViews = communityDal.QueryCommunityByCondition(province, city, county, ownerid, hospital);

                //redisTestDal.Set<List<CommunityView>>(keyName, communityViews);
            }

            return communityViews;
        }

        /// <summary>
        /// 通过名称、省市区获取社区信息
        /// </summary>
        /// <param name="province">省名称</param>
        /// <param name="city">市名称</param>
        /// <param name="county">区名称</param>
        /// <param name="communityName"></param>
        /// <returns></returns>
        public List<CommunityView> GetCommunityByName(string province, string city, string county, string communityName, string hospital)
        {
            return communityDal.QueryCommunityByConditionName(communityName, province, county, null, city, null, null, null, 0,hospital);
        }
    }
}
